package com.zg.ktcomwan.fragment

import android.os.CountDownTimer
import android.view.View
import android.view.WindowManager
import android.widget.Button
import androidx.navigation.Navigation
import com.zg.ktcomwan.HomeActivity
import com.zg.ktcomwan.R
import com.zg.ktcomwan.base.BaseFragment
import com.zg.ktcomwan.utils.ScreenUtils
import kotlinx.android.synthetic.main.fragment_welcome.*

/***
 * @Author:      zxd
 * @CreateDate:  2021/8/26 17:09
 * @Description: 欢迎碎片
 */
open class WelcomeFragment : BaseFragment() {
    override fun getLayoutId(): Int {
        return R.layout.fragment_welcome
    }

    override fun afterCreate() {
        activity?.let { ScreenUtils().hideBottomUIMenu(it) }
        mJumpbtn = jump_btn

        countDownTimer = MyCountDownTimer(3000 + 200, 1000)
        (countDownTimer as MyCountDownTimer).start()

        jump_btn.visibility = View.VISIBLE
        jump_btn.setOnClickListener {
            goMainActivity()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (countDownTimer != null) {
            countDownTimer!!.cancel()
        }
    }

    var isGoMain = false

    companion object {
        private var countDownTimer //计时器
                : CountDownTimer? = null

        private var mJumpbtn: Button? = null
    }

    /**
     * 倒计时计时器
     */
    inner class MyCountDownTimer
    /**
     * @param millisInFuture    The number of millis in the future from the call
     * to [.start] until the countdown is done and [.onFinish]
     * is called.
     * @param countDownInterval The interval along the way to receive
     * [.onTick] callbacks.
     */
        (millisInFuture: Long, countDownInterval: Long) :
        CountDownTimer(millisInFuture, countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            mJumpbtn!!.setText("跳过(" + millisUntilFinished / 1000 + "s)")
        }

        override fun onFinish() {
            mJumpbtn!!.setText("跳过(" + 0 + "s)")
            goMainActivity()
        }


    }

    fun goMainActivity() {
        if (isGoMain) return
        welcomecl.visibility = View.GONE
        val navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment)
        //navController.popBackStack(R.id.navigation_welcome, true)
        requireActivity().window.setBackgroundDrawableResource(R.color.white)
        //navController.navigate(R.id.goHome)
        requireActivity().window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        requireActivity().window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE

        //这里使用childFragmentManager会出错 https://blog.csdn.net/moxiouhao/article/details/81315418
        val frg = parentFragmentManager!!.fragmentFactory.instantiate(
            requireContext().classLoader, "com.zg.ktcomwan.fragment.ListFragment"
        )
        parentFragmentManager.beginTransaction()
            .replace(R.id.nav_host_fragment, frg)
            .setPrimaryNavigationFragment(frg).commit()

//        navController.navigate(R.id.navigation_article)
        (activity as HomeActivity).initView()
        isGoMain = true
    }
}